//泛型  

//  创建一个函数，返回一个数组   

// 不用泛型
/* function createArray(count:number,value:any):any[]{
    let arr:any[] = [];
    for(let i = 0; i< count;i++){
        arr.push(value);
    }

    return arr;
}

let newarr = createArray(10,"学习ts"); */
// let newarr1 = createArray(10,Math.PI);

// console.log(newarr);

// console.log(newarr[2].slice(0,2));  //没有提示，也不报错，但可以正常运行
// console.log(newarr[3].toFixed(3));   //没有提示，也不报错，也不能正常运行
// console.log(newarr[100].slice(0,2));  // 没有提示，也不报错，也不能正常运行




/*************************   使用泛型  ****************************** */
// 泛型  创建是不知道类型，但是使用的时候知道类型
// 泛型参数：  T P R V
function createArray<T>(count:number,value:T):T[]{
    let arr:T[] = [];
    for(let i = 0; i< count;i++){
        arr.push(value);
    }

    return arr;
}

let arr =  createArray<string>(10,"学习ts");

arr[2].slice(0,2);   // 不报错，还有提示
// arr[3].toFixed(2);   //报错

let arr1 = createArray<number>(10,666);
arr1[0].toFixed(2);


/*****************多个泛型参数*********************/
                                // 元祖
function createArr<T,R>(a:T,b:R):[T,R]{
    return [a,b];
}

let arr2 = createArr<string,number>("正在学习泛型",3.14154926);

arr2[0].slice(0,3);
arr2[1].toFixed(2);

/**************************泛型 接口****************************** */

//  axios 返回成功的完整的响应体

//axios响应成功的完整结构
interface Response<T>{
    headers:object,
    config:object,
    status:number,
    statusText:string
    data:T
}

// 学生接口
interface Student{
    id:number,
    name:string,
    age:number,
    sex:string
}

// 书籍的接口
interface Book {
    id:number,
    name:string,
    price:number
}

//  data 数据的接口
interface Data<T>{
    code:number,
    message:string,
    data:T
}

//删除成功
let del:Response<string> = {
    headers:{},
    config:{},
    status:200,
    statusText:"成功",
    data:"删除成功"
}



//单个学生的信息
let result:Response<Student> = {
    headers:{},
    config:{},
    status:200,
    statusText:"成功",
    data:{
        id:1,
        name:"一千零一夜",
        age:18,
        sex:"男"
    }
}

//单个书籍的信息
let result1:Response<Book> = {
    headers:{},
    config:{},
    status:200,
    statusText:"成功",
    data:{
        id:1,
        name:"一千零一夜",
        price:1000
    }
}


// 进一步演化单个学生信息
let result2:Response<Data<Student>> = {
    status:200,
    headers:{},
    config:{},
    statusText:"",
    data:{
        code:200,
        message:"ok",
        data:{
            id:1,
            name:"张三",
            sex:"男",
            age:18
        }
    }
}



// 进一步演化单个书籍信息
let result3:Response<Data<Book>> = {
    headers:{},
    config:{},
    status:200,
    statusText:"",
    data:{
        code:200,
        message:"ok",
        data:{
            id:1,
            name:"124234",
            price:1000
        }
    }
}


//多个学生信息
let result4:Response<Data<Student[]>> = {
    headers:{},
    config:{},
    status:200,
    statusText:"",
    data:{
        code:200,
        message:"ok",
        data:[
            {id:1,name:"张三",age:18,sex:"男"},
            {id:1,name:"张三",age:18,sex:"男"},
            {id:1,name:"张三",age:18,sex:"男"},
            {id:1,name:"张三",age:18,sex:"男"},
            {id:1,name:"张三",age:18,sex:"男"},
            {id:1,name:"张三",age:18,sex:"男"},
            {id:1,name:"张三",age:18,sex:"男"}
        ]
    }
}


// 书籍数据 复数结构

let result5:Response<Data<Book[]>> = {
    headers:{},
    config:{},
    status:200,
    statusText:"",
    data:{
        code:200,
        message:"ok",
        data:[
            {id:100,name:"1111",price:1000},
            {id:100,name:"1111",price:1000},
            {id:100,name:"1111",price:1000},
            {id:100,name:"1111",price:1000},
            {id:100,name:"1111",price:1000},
            {id:100,name:"1111",price:1000},
            {id:100,name:"1111",price:1000},
            {id:100,name:"1111",price:1000},
            {id:100,name:"1111",price:1000},
            {id:100,name:"1111",price:1000}
        ]
    }
}




// console.log(result);

// console.log(result1);

// console.log(result2)

// console.log(result3)

// console.log(result4);

console.log(result5);


/************************** 泛型类   ******************************8 */
class Container<T>{
    store:T[];
    constructor(store:T[]){
        this.store = store;
    }
}

let c = new Container<string>(["abc","abc"]);
console.log(c.store);

let c1 = new Container<Book>([{id:1,name:"平凡的世界",price:100},{id:1,name:"平凡的世界",price:100},{id:1,name:"平凡的世界",price:100},{id:1,name:"平凡的世界",price:100},{id:1,name:"平凡的世界",price:100}])

console.log(c1.store);


/*****************   泛型约束  ***************************/
//声明一个接口  是否length 属性
interface LengthWise{
    length:number
}

function demo<T  extends LengthWise>(x:T):void{
    console.log(x.length);
}

console.log(demo<string[]>(["abc"]));



/******************** type  类型  ********************* */

let students:Student[] = [
    {id:1,name:"张三",age:18,sex:"男"},
    {id:1,name:"张三",age:18,sex:"男"},
    {id:1,name:"张三",age:18,sex:"男"},
    {id:1,name:"张三",age:18,sex:"男"},
    {id:1,name:"张三",age:18,sex:"男"},
    {id:1,name:"张三",age:18,sex:"男"},
    {id:1,name:"张三",age:18,sex:"男"},
    {id:1,name:"张三",age:18,sex:"男"},
    {id:1,name:"张三",age:18,sex:"男"}
];

type Students = Student[];


let sArr:Students = [
    {id:1,name:"张三",age:18,sex:"男"},
    {id:1,name:"张三",age:18,sex:"男"},
    {id:1,name:"张三",age:18,sex:"男"},
    {id:1,name:"张三",age:18,sex:"男"},
    {id:1,name:"张三",age:18,sex:"男"}
] 
























export{}